//@ sourceURL=core-item-script-selector.js
limsTest.scriptSelectorPage = {

    getBtnToolbar: function () {
        var _this = this;
        return {
            type: 'btnToolbar',
            items: [{
                type: 'button',
                text: 'GIKAM.BUTTON.CONFIRM',
                class: 'blue',
                icon: 'save',
                color: 'white',
                onClick: function () {
                    if (Gikam.getComp('core-test-item-script-selector-item-form').validate()) {
                        Gikam.getLastModal().close(_this.form.getData().expression);
                    }
                }
            }]
        }
    },

    getFormParam: function () {
        var _this = this;
        return {
            type: 'form',
            columns: 1,
            id: 'core-test-item-script-selector-form',
            fields: [{
                field: 'expression',
                title: 'T_CORE_ITEM.EXPRESSION',
                readonly: true
            }],
            onRendered: function () {
                _this.form = this;
            }
        };
    },

    getTabParam: function () {
        var _this = this;
        return {
            type: 'tab',
            shrink: false,
            panels: [{
                id: 'core-test-item-script-selector-item-panel',
                title: 'CORE.MODULE.SCRIPT.TESTS.TAB.ITEM',
                items: [{
                    type: 'form',
                    columns: 2,
                    id: 'core-test-item-script-selector-item-form',
                    fields: [{
                        field: 'itemName',
                        title: 'T_CORE_ITEM.ITEM',
                        type: 'select',
                        search: true,
                        colspan: 2,
                        validators: ['notEmpty'],
                        firstBlank: false,
                        onChange: function (field, value, rowIndex, oldValue, text) {
                            _this.setExpression();
                        }
                    }, {
                        field: 'category',
                        title: 'T_LIMS_TEST_METHOD_ANALYTE.ANALTYPE',
                        type: 'select',
                        validators: ['notEmpty'],
                        firstBlank: false,
                        category: 'sys',
                        param: {
                            codeCategoryId: 'analyteResultCategory'
                        },
                        onChange: function (field, value, rowIndex, oldValue, text) {
                            _this.setExpression();
                        }
                    }],
                    onRendered: function () {
                        _this.fillItemSelectOption(_this.param.testId, _this.param.itemId, _this.param.type);
                    }
                }]
            }]
        };
    },

    fillItemSelectOption: function (testId, itemId, type) {
        if (type === 'sampleItem') {
            Gikam.post(IFM_CONTEXT + '/secure/baseline/samplemanage/samples/select/formula', Gikam.getJsonWrapper({
                f: {
                    id_EQ: '1'
                },
                n: 1,
                s: 1
            }, [])).done(function (data) {
                var map = data.columnTypeMap;
                console.log(map);
                let items = [];
                for (var key in map) {
                    items.push({
                        text: key + '-' + Gikam.propI18N('T_LIMS_SAMPLE.' + key.toUpperCase()),
                        value: key
                    })
                }
                console.log(items);
                Gikam.getComp('core-test-item-script-selector-item-form').setSelectOptions('itemName', items);
            });
            return;
        } else if (type === 'item') {
            Gikam.post(limsTest.methodAnalytesUrl + '/queries', Gikam.getJsonWrapper({
                f: {
                    testMethodId_EQ: testId,
                    id_DIFFER: Gikam.isEmpty(itemId) ? '' : itemId
                }
            }, [])).done(function (data) {

                data = data.rows;

                var selectData = data.map(function (item) {
                    var selectItem = {};

                    selectItem.text = item.analyte;
                    selectItem.value = item.analyte;

                    return selectItem;
                });
                Gikam.getComp('core-test-item-script-selector-item-form').setSelectOptions('itemName', selectData);
            });
            return;
        } else if (type === 'purpose') {
            Gikam.post(limsTest.productPurposeUrl + '/queries', Gikam.getJsonWrapper({
                f: {
                    productMainId_EQ: testId,
                    id_DIFFER: Gikam.isEmpty(itemId) ? '' : itemId
                }
            }, [])).done(function (data) {

                data = data.rows;

                var selectData = data.map(function (item) {
                    var selectItem = {};

                    selectItem.text = item.ext$.purposename;
                    selectItem.value = item.ext$.purposename;

                    return selectItem;
                });
                Gikam.getComp('core-test-item-script-selector-item-form').setSelectOptions('itemName', selectData);
            });
            return;
        }
    },

    setExpression: function () {
        var _this = this;

        var expression = '';

        let type = _this.param.type;
        if (type === 'item') {
            expression = _this.getItemExpression();
        } else if (type === 'sampleItem') {
            expression = _this.getSampleItemExpression();
        } else if (type === 'purpose') {
            expression = _this.getItemExpression();
        }

        _this.form.setData({
            expression: expression
        });

    },

    getSampleItemExpression: function () {
        var itemData = Gikam.getComp('core-test-item-script-selector-item-form').getData();
        var expression = '';
        if (Gikam.isEmpty(itemData.itemName)) {
            return '';
        }
        expression = itemData.itemName;
        if (Gikam.isNotEmpty(itemData.category)) {
            if (itemData.category === 'N') {
                expression = 'SN("' + expression + '")';
            } else {
                expression = 'SS("' + expression + '")';
            }
        }
        return expression;
    },

    getItemExpression: function () {
        var _this = this;

        var itemData = Gikam.getComp('core-test-item-script-selector-item-form').getData();
        var expression = '';
        if (Gikam.isEmpty(itemData.itemName)) {
            return '';
        }
        expression = itemData.itemName;
        if (Gikam.isNotEmpty(itemData.category)) {
            if (itemData.category === 'N') {
                expression = 'RN("' + expression + '")';
            } else {
                expression = 'RS("' + expression + '")';
            }
        }

        return expression;

    },

    createPage: function () {
        Gikam.create('layout', {
            center: {
                items: [this.getBtnToolbar(), this.getFormParam(), this.getTabParam()]
            },
            renderTo: Gikam.getLastModal().window.$dom
        });
    },

    init: function (param) {
        this.param = param;

        this.testName = null;

        this.createPage();
    }
}
